//
//  YMHomeErrorUploadAlert.m
//  YMMedical
//
//  Created by y s on 2023/7/5.
//

#import "YMHomeErrorUploadAlert.h"


@interface YMHomeErrorUploadAlert ()

@property (nonatomic, strong) UIView *vAlert;
@property (nonatomic, strong) UITextField *tfMain;

@property (nonatomic, strong) UIButton *btnCancel;
@property (nonatomic, strong) UIButton *btnSure;

@end

@implementation YMHomeErrorUploadAlert

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    self.backgroundColor = [SUIUtil colorOf:0x000000 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self addSubview:self.vAlert];
    [self.vAlert addSubview:self.tfMain];
    [self.vAlert addSubview:self.btnCancel];
    [self.vAlert addSubview:self.btnSure];
    
    [self.vAlert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.mas_equalTo([SFitUtil fit:-30]);
        make.width.mas_equalTo([SFitUtil fit:290]);
    }];
    [self.tfMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:20]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:40]);
    }];

    
    [self.btnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:0]);
        make.top.equalTo(self.tfMain.mas_bottom).offset([SFitUtil fit:15]);
        make.bottom.mas_equalTo([SFitUtil fit:-3]);
        make.height.mas_equalTo([SFitUtil fit:46]);
    }];
    [self.btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:0]);
        make.centerY.height.width.equalTo(self.btnSure);
        make.right.equalTo(self.btnSure.mas_left);
    }];
    
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    [self addGestureRecognizer:tap];
}

- (void)show:(UIView *)view{
    for (UIView *item in view.subviews) {
        if ([item isKindOfClass:[self class]]){
            return;
        }
    }
    [view addSubview:self];
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

- (void)hide{
    [self.tfMain resignFirstResponder];
    [self removeFromSuperview];
}

- (void)sureClickAction{
    [self.tfMain resignFirstResponder];
    if (self.uploadBlock){
        self.uploadBlock(self.tfMain.text);
    }
    [self hide];
}
- (void)tapClick{
    [self.tfMain resignFirstResponder];
}

#pragma mark -- setter and getter
- (UIView *)vAlert{
    if (_vAlert == nil){
        _vAlert = [[UIView alloc] init];
        _vAlert.backgroundColor = STheme.D9;
        _vAlert.clipsToBounds = YES;
        _vAlert.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _vAlert;
}
- (UITextField *)tfMain{
    if (_tfMain == nil){
        _tfMain = [[UITextField alloc] init];
        _tfMain.borderStyle = UITextBorderStyleNone;
        _tfMain.textColor = [SUIUtil colorOf:0x333333];
        _tfMain.font = [SFitUtil fitFont:14];
        _tfMain.returnKeyType = UIReturnKeyDone;
        _tfMain.keyboardType = UIKeyboardTypeDefault;
        _tfMain.clearButtonMode = NO;
        _tfMain.backgroundColor = STheme.D7;
//        _tfMain.borderStyle = UITextBorderStyleLine;
        
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"请输入异常" attributes:@{NSFontAttributeName:[SFitUtil fitFont:14],NSForegroundColorAttributeName:[SUIUtil colorOf:0xDCDCDC]}];
        _tfMain.attributedPlaceholder = attr;
    }
    return _tfMain;
}
- (UIButton *)btnSure{
    if (_btnSure == nil) {
        _btnSure = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnSure setTitle:@"异常上报" forState:UIControlStateNormal];
        [_btnSure setTitleColor:STheme.D12 forState:UIControlStateNormal];
        _btnSure.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnSure addTarget:self action:@selector(sureClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnSure;
}
- (UIButton *)btnCancel{
    if (_btnCancel == nil) {
        _btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCancel setTitle:KLanguage(@"cancle_txt") forState:UIControlStateNormal];
        [_btnCancel setTitleColor:STheme.D4 forState:UIControlStateNormal];
        _btnCancel.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnCancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnCancel;
}

@end
